/**
 * JimingLiu created the file at 2006-12-19 下午05:53:12
 * $Id$
 */
package jm.lib.common.db;

/**
 * @author JimingLiu
 *
 */
public class CompositeSearchCondition implements SearchCondition {
    private boolean or;
    private SearchCondition[] conditions;
    
    public CompositeSearchCondition(SearchCondition ... conditions) {
        this(true, conditions);
    }
    
    CompositeSearchCondition(boolean isOr, SearchCondition ... conditions) {
        or = isOr;
        this.conditions = conditions;
    }

    /**
     * @return the or
     */
    public boolean isOr() {
        return or;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        if(null == conditions && conditions.length == 0) return "";
        String connector = or?" OR ":" AND ";
        StringBuilder sb = new StringBuilder();
        sb.append('(');
        sb.append(conditions[0].toString());
        for (int i = 1; i < conditions.length; i++) {
            sb.append(connector).append(conditions[i].toString());
        }
        sb.append(')');
        return super.toString();
    }

}
